package com.example.dense_rack_server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.dense_rack_server.entity.ArchivesArea;
import com.example.dense_rack_server.entity.ArchivesType;
import com.example.dense_rack_server.entityVo.MenuALLVo;
import com.example.dense_rack_server.entityVo.MyAreaTreeVo;
import com.example.dense_rack_server.mapper.ArchivesAreaMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dense_rack_server.mapper.HostMapper;
import com.example.dense_rack_server.utils.DateFormat;
import com.example.dense_rack_server.utils.ResponseModel;
import com.example.dense_rack_server.utils.TreeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案区域 服务实现类
 * </p>
 *
 * @author 张彬
 * @since 2025-03-04
 */
@Service
public class ArchivesAreaService extends ServiceImpl<ArchivesAreaMapper, ArchivesArea> {

    @Resource
    private ArchivesAreaMapper archiveAreaMapper;


    /**
     * 绘制区域树
     */
    public Map<String, Object> buildAreaTree() {
        List<MyAreaTreeVo> myDeptTreeVoList = archiveAreaMapper.buildAres();
        List<MyAreaTreeVo> resultList = TreeUtil.buildAreaTreeAll(myDeptTreeVoList);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("message", 200);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("status", map);
        resultMap.put("data", resultList);
        return resultMap;
    }

    /**
     * 绘制区域树(新增使用)
     */
    public ResponseModel buildAreaTreeTable() {
        List<MenuALLVo> sysUserVoList = archiveAreaMapper.buildAreaTree();
        return ResponseModel.success(sysUserVoList);
    }

    /**
     * 新增区域保存
     */
    public ResponseModel saveArea(JSONObject data) {
        QueryWrapper<ArchivesArea> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.eq("area_name", data.getStr("areaName"));
        ArchivesArea archiveArea = archiveAreaMapper.selectOne(deptQueryWrapper);
        if (archiveArea != null) {
            return ResponseModel.fail("区域名称重复");
        }
        ArchivesArea insertArea = new ArchivesArea();
        if (StrUtil.isEmpty(data.getStr("parentId"))) {
            insertArea.setParentId(0);
        } else {
            insertArea.setParentId(data.getInt("parentId"));
        }
        insertArea.setAreaName(data.getStr("areaName"));
        insertArea.setSort(data.getInt("sort"));
        insertArea.setStatus(data.getInt("status"));
        insertArea.setCreateTime(DateFormat.getTime());
        insertArea.setUpdateTime(DateFormat.getTime());
        insertArea.setDeleteStatus(0);
        int insertStatus = archiveAreaMapper.insert(insertArea);
        return insertStatus > 0 ? ResponseModel.success("新增  " + data.getStr("areaName") + "  区域成功") : ResponseModel.fail("新增区域失败");
    }

    /**
     * 删除功能档案
     */
    public ResponseModel deleteArea(String id) {
        String[] strings = id.split(",");
        List<String> idList = CollUtil.newArrayList(strings);
        archiveAreaMapper.deleteBatchIds(idList);
        return ResponseModel.success("删除区域成功");
    }



    /**
     * 区域修改
     */
    public ResponseModel updateArea(Map<String, Object> data) {
        UpdateWrapper<ArchivesArea> updateWrapper = new UpdateWrapper<ArchivesArea>();
        updateWrapper.eq("id", Convert.toInt(data.get("id")));
        updateWrapper.set("area_name", Convert.toStr(data.get("areaName")));
        updateWrapper.set("sort", Convert.toInt(data.get("sort")));
        updateWrapper.set("status", Convert.toInt(data.get("status")));
        updateWrapper.set("parent_id", Convert.toInt(data.get("parentId")));
        int updateStatus = archiveAreaMapper.update(null, updateWrapper);
        return updateStatus > 0 ? ResponseModel.success("修改区域成功") : ResponseModel.fail("修改区域失败");
    }



    /**
     * 档案分类列表
     */
    public Map<String, Object> areaList() {
        QueryWrapper<ArchivesArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        List<ArchivesArea> list = archiveAreaMapper.selectList(queryWrapper);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", "请求成功");
        resultMap.put("data", list);
        resultMap.put("count", 1000);
        return resultMap;
    }

}
